// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        item:[cc.Node],
        itemPosition:[],
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.currentLevel =4;
        this.reset(this.currentLevel);
        let basePosition = [];

        for(let i=0;i< this.item.length;i++){
            let position = this.item[i].getPosition();
            basePosition.push(position);
        }
        this.itemPosition = basePosition;
    },

    start () {

    },
    reset(currentLevel){
        let startLevel = currentLevel-2;
        for(let i=0;i< this.item.length;i++){
            if(this.itemPosition[i]){
                this.item[i].setPosition(this.itemPosition[i]);
            }
            let js = this.item[i].getComponent('levelItem');
            js.setNumber(startLevel+i);
            js.setBoss(startLevel+i);
            js.setIndex(i);
            js.setPointShow();
            this.item[i].opacity = 255;
            this.item[i].stopAllActions;
            this.item[i].setScale(cc.v2(1,1));


        }
        this.item[0].opacity =0;
        this.item[4].opacity =0;
        this.item[2].setScale(cc.v2(1.3,1.3));

    },
    play(){

    },
    //播放关卡移动的动画
    nextLevel(){
        let dly = cc.delayTime(0.5);
        for(let i=1;i<this.item.length;i++){
            let js = this.item[i].getComponent('levelItem');
            if(this.itemPosition[i]!==undefined){
                this.item[i].setPosition(this.itemPosition[i]);
            }
            let moveTo = cc.moveTo(0.5,cc.v2(this.itemPosition[i-1]));
            let fadeOut = cc.fadeOut(0.5);
            let fadeIn = cc.fadeIn(0.5);
            let scaleSmall = cc.scaleTo(0.5,1);
            let scalebig = cc.scaleTo(0.5,1.3);


            if(i===1){
                let action = cc.spawn(fadeOut,moveTo);
                this.item[i].runAction(action);
                js.playPoint(-1);
            } else if(i===2){
                let action = cc.spawn(scaleSmall,moveTo);
                this.item[i].runAction(action);
                js.playPoint(1);
            } else if(i ===3){
                let action = cc.spawn(scalebig,moveTo);
                this.item[i].runAction(action);
                js.playPoint(0);
            } else if(i===4){
                let action = cc.spawn(fadeIn,moveTo);
                this.item[i].runAction(action);
                js.playPoint(-1);
            } else{
                this.item[i].runAction(moveTo);

            }
        }
        // this.currentLevel++;
        // this.reset(this.currentLevel );

        // this.node.runAction(dly);

        let nodeSeq = cc.sequence(dly,cc.callFunc(
            function(){
                this.currentLevel++;
                this.reset(this.currentLevel );
            }.bind(this)
        ));
        this.node.runAction(nodeSeq);
    },
    moveOut:function(){
        let levelAction = cc.spawn(cc.scaleTo(0.5,0.4,0.4),cc.moveTo(0.5,0,645));
        this.node.runAction(levelAction).easing(cc.easeBackIn());
    },
    moveIn:function(){
        let levelAction = cc.spawn(cc.scaleTo(0.5,1,1),cc.moveTo(0.5,0,174));
        this.node.runAction(levelAction).easing(cc.easeBackOut());
    },
    // update (dt) {},
});
